//
// Created by wuzhe on 2022/3/30.
//

#ifndef UNIVERSEROOM_ROOM_HH
#define UNIVERSEROOM_ROOM_HH

#include <string>
#include <list>
#include <unordered_map>
#include <sstream>
#include <utility>
#include <deque>
#include "Utils.h"

class Msg;
class Player;
class Space;
/**
 * 聊天室房间
 */
class Room {
private:
    std::unordered_map<std::string, Player*> playerMap;
    Player *owner= nullptr;
    Space *space = nullptr;
public:
    std::string roomName;
    std::deque<std::string> msgQueue;
    int roomId;
    // player create room
    Room(Space* space, std::string roomName, Player* player, int id);
    // space load room
    explicit Room(Space* space, std::string roomName, int id):space(space), roomId(id), roomName(roomName), playerMap() {
        onCreate();
        initHistory(10);
    }
    ~Room();
    // 观察者模式
    void broadcast(Msg msg);
    void onPlayerEnter(Player*);
    void onPlayerQuit(Player*);
    void onCreate();
    void onDelete();
    void onTalk(std::string msg, Player& player);
    const std::deque<std::string>& getHistory() const;

private:
    void initHistory(int count);

};

class Msg {
public:
    std::string context;
    std::string roleName;
    std::string time;
    Msg(std::string context, std::string roleName, std::string time):context(context),roleName(roleName),time(time) {
    }
    std::string toString() const {
        std::stringstream sstream;
        sstream << "[" << roleName << "]:" << context;
        return sstream.str();
    }
};


#endif //UNIVERSEROOM_ROOM_HH
